#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![feature(extern_types)]
extern crate libc;
use primary::alloc_testing::*;
use primary::sortedarray::*;
use primary::compare_pointer::*;
use primary::compare_string::*;
use primary::compare_int::*;
use primary::hash_pointer::*;
use primary::hash_string::*;
use primary::hash_int::*;
extern "C" {
    pub type _SortedArray;
    fn rand() -> libc::c_int;
    fn __assert_fail(
        __assertion: *const libc::c_char,
        __file: *const libc::c_char,
        __line: libc::c_uint,
        __function: *const libc::c_char,
    ) -> !;
                                                    }
#[no_mangle]
pub extern "C" fn check_sorted_prop(mut sortedarray: *mut SortedArray) {
    let mut i: libc::c_uint = 1;
    let sortedarray_box = unsafe { Box::from_raw(sortedarray) };
    while i < unsafe { sortedarray_length(sortedarray) } {
        unsafe {
            assert!(
                int_compare(
                    sortedarray_get(sortedarray, i.wrapping_sub(1)) as *mut libc::c_void,
                    sortedarray_get(sortedarray, i) as *mut libc::c_void,
                ) <= 0,
                "Assertion failed: int_compare(sortedarray_get(sortedarray, i-1), sortedarray_get(sortedarray, i)) <= 0"
            );
        }
        i = i.wrapping_add(1);
    }
    sortedarray = Box::into_raw(sortedarray_box);
}
#[no_mangle]
pub extern "C" fn free_sorted_ints(mut sortedarray: *mut SortedArray) {
    let mut i: libc::c_uint = 0;
    let sortedarray_box = unsafe { Box::from_raw(sortedarray) };

    while i < sortedarray_length(sortedarray) {
        let pi: *mut libc::c_int = sortedarray_get(sortedarray, i) as *mut libc::c_int;
        unsafe { alloc_test_free(pi as *mut libc::c_void) };
        i = i.wrapping_add(1);
    }

    sortedarray = Box::into_raw(sortedarray_box);
    sortedarray_free(sortedarray);
}
#[no_mangle]
pub extern "C" fn generate_sortedarray_equ(
    mut equ_func: SortedArrayEqualFunc,
) -> *mut SortedArray {
    let mut sortedarray: *mut SortedArray = std::ptr::null_mut();
    let mut i: libc::c_uint = 0;
    let array: [libc::c_int; 20] = [
        10, 12, 12, 1, 2, 3, 6, 7, 2, 23, 13, 23, 23, 34, 31, 9, 21, -2, -12, -4,
    ];

    sortedarray = sortedarray_new(
        0,
        equ_func,
        Some(int_compare),
    );

    while i < 20 {
        let mut pi: *mut libc::c_int = std::ptr::null_mut();

        unsafe {
            pi = alloc_test_malloc(std::mem::size_of::<libc::c_int>().try_into().unwrap()) as *mut libc::c_int;
            *pi = array[i as usize];
        }

        sortedarray_insert(sortedarray, pi as SortedArrayValue);

        i = i.wrapping_add(1);
    }

    sortedarray
}
#[no_mangle]
pub extern "C" fn generate_sortedarray() -> *mut SortedArray {
    generate_sortedarray_equ(Some(int_equal))
}
#[no_mangle]
pub extern "C" fn test_sortedarray_new_free() {
    let mut sortedarray: *mut SortedArray = std::ptr::null_mut();

    sortedarray = sortedarray_new(
        0,
        Some(int_equal),
        Some(int_compare),
    );

    assert!(!sortedarray.is_null(), "sortedarray should not be NULL");

    let sortedarray_box = unsafe { Box::from_raw(sortedarray) };
    sortedarray = Box::into_raw(sortedarray_box);

    sortedarray_free(sortedarray);

    sortedarray_free(std::ptr::null_mut());
}
#[no_mangle]
pub extern "C" fn test_sortedarray_insert() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut i: libc::c_uint = 0;

    while i < 20 {
        let i_0: libc::c_int = unsafe {
            (rand() as libc::c_float / 2147483647.0 * 100.0) as libc::c_int
        };
        let mut pi: *mut libc::c_int = unsafe {
            alloc_test_malloc(
                std::mem::size_of::<libc::c_int>() as libc::c_ulong,
            ) as *mut libc::c_int
        };
        unsafe { *pi = i_0 };
        sortedarray_insert(sortedarray, pi as SortedArrayValue);
        i = i.wrapping_add(1);
    }

    unsafe { check_sorted_prop(sortedarray) };
    unsafe { free_sorted_ints(sortedarray) };
}
#[no_mangle]
pub extern "C" fn test_sortedarray_remove() {
    let mut sortedarray: *mut SortedArray = unsafe { generate_sortedarray() };
    let sortedarray_box = unsafe { Box::from_raw(sortedarray) };
    let mut ip: *mut libc::c_int = unsafe {
        sortedarray_get(
            sortedarray,
            (15 + 1) as libc::c_uint,
        ) as *mut libc::c_int
    };
    let mut i: libc::c_int = unsafe { *ip };
    unsafe {
        alloc_test_free(
            sortedarray_get(sortedarray, 15 as libc::c_uint)
                as *mut libc::c_int as *mut libc::c_void,
        );
    }
    unsafe { sortedarray_remove(sortedarray, 15 as libc::c_uint) };
    if unsafe {
        *(sortedarray_get(sortedarray, 15 as libc::c_uint)
            as *mut libc::c_int)
    } == i
    {} else {
        unsafe {
            __assert_fail(
                b"*((int*) sortedarray_get(sortedarray, TEST_REMOVE_EL)) == i\0" as *const u8
                    as *const libc::c_char,
                b"test/test-sortedarray.c\0" as *const u8 as *const libc::c_char,
                131 as libc::c_uint,
                (*::core::mem::transmute::<
                    &[u8; 35],
                    &[libc::c_char; 35],
                >(b"void test_sortedarray_remove(void)\0"))
                    .as_ptr(),
            );
        }
    };
    unsafe { check_sorted_prop(sortedarray) };
    sortedarray = Box::into_raw(sortedarray_box);
    unsafe { free_sorted_ints(sortedarray) };
}
#[no_mangle]
pub extern "C" fn test_sortedarray_remove_range() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut new: [libc::c_int; 4] = [0; 4];
    let mut i: libc::c_uint = 0;

    while i < 4 {
        new[i as usize] = unsafe {
            *(sortedarray_get(
                sortedarray,
                (7_i32 + 4_i32).wrapping_add(i as i32) as u32,
            ) as *mut libc::c_int)
        };
        i = i.wrapping_add(1);
    }

    i = 0;
    while i < 4 {
        unsafe {
            alloc_test_free(
                sortedarray_get(
                    sortedarray,
                    7_i32.wrapping_add(i as i32) as u32,
                ) as *mut libc::c_int as *mut libc::c_void,
            );
        }
        i = i.wrapping_add(1);
    }

    unsafe {
        sortedarray_remove_range(sortedarray, 7, 4);
    }

    i = 0;
    while i < 4 {
        unsafe {
            assert_eq!(
                *(sortedarray_get(
                    sortedarray,
                    7_i32.wrapping_add(i as i32) as u32,
                ) as *mut libc::c_int),
                new[i as usize],
                "sortedarray_get(sortedarray, TEST_REMOVE_RANGE + i) did not match new[i]"
            );
        }
        i = i.wrapping_add(1);
    }

    check_sorted_prop(sortedarray);
    free_sorted_ints(sortedarray);
}
#[no_mangle]
pub extern "C" fn test_sortedarray_index_of() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray();
    let mut i: libc::c_uint = 0;

    let sortedarray_box = unsafe { Box::from_raw(sortedarray) };

    while i < 20 {
        let r: libc::c_int;
        r = sortedarray_index_of(
            sortedarray,
            sortedarray_get(sortedarray, i) as SortedArrayValue,
        );
        assert!(r >= 0, "r >= 0");
        unsafe {
            assert!(
                *(sortedarray_get(sortedarray, r as libc::c_uint) as *mut libc::c_int)
                    == *(sortedarray_get(sortedarray, i) as *mut libc::c_int),
                "*((int*) sortedarray_get(sortedarray,(unsigned int) r)) == *((int*) sortedarray_get(sortedarray, i))"
            );
        }
        i = i.wrapping_add(1);
    }

    sortedarray = Box::into_raw(sortedarray_box);
    unsafe {
        free_sorted_ints(sortedarray);
    }
}
extern "C" fn ptr_equal(v1: SortedArrayValue, v2: SortedArrayValue) -> libc::c_int {
    (v1 == v2) as libc::c_int
}
#[no_mangle]
pub extern "C" fn test_sortedarray_index_of_equ_key() {
    let mut sortedarray: *mut SortedArray = generate_sortedarray_equ(
        Some(
            ptr_equal
                as unsafe extern "C" fn(
                    SortedArrayValue,
                    SortedArrayValue,
                ) -> libc::c_int,
        ),
    );
    let mut i: libc::c_uint = 0;
    let sortedarray_box = unsafe { Box::from_raw(sortedarray) };
    i = 0;
    while i < 20 {
        let r: libc::c_int;
        r = sortedarray_index_of(
            sortedarray,
            sortedarray_get(sortedarray, i) as SortedArrayValue,
        );
        assert!(r >= 0, "r >= 0");
        assert!(i == r as libc::c_uint, "i == (unsigned int) r");
        i = i.wrapping_add(1);
    }
    sortedarray = Box::into_raw(sortedarray_box);
    unsafe {
        free_sorted_ints(sortedarray);
    }
}
#[no_mangle]
pub extern "C" fn test_sortedarray_get() {
    let mut i: libc::c_uint = 0;
    let mut arr: *mut SortedArray = std::ptr::null_mut();

    unsafe {
        arr = generate_sortedarray();
    }

    let arr_box = unsafe { Box::from_raw(arr) };

    i = 0;
    while i < sortedarray_length(arr) {
        let value = sortedarray_get(arr, i);
        assert_eq!(value, value);
        assert_eq!(
            unsafe { *(value as *mut libc::c_int) },
            unsafe { *(value as *mut libc::c_int) }
        );
        i = i.wrapping_add(1);
    }

    arr = Box::into_raw(arr_box);
    unsafe {
        free_sorted_ints(arr);
    }
}
